package com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

public class AftApplyOrderLineFactory {

    /**
     * 初始化基础属性值
     */
    public static AftApplyOrderLine initBaseFields(AftApplyOrderLine aftApplyOrderLine) {
        if (aftApplyOrderLine.getUuid() == null) {
            aftApplyOrderLine.setUuid(UUIDUtils.generateUUID());
            aftApplyOrderLine.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(aftApplyOrderLine.getSerAudit())) {
                aftApplyOrderLine.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(aftApplyOrderLine.getSerUsing())) {
                aftApplyOrderLine.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            aftApplyOrderLine.setLastModifyTime(LocalDateTime.now());
        }
        return aftApplyOrderLine;
    }

    /**
     * 初始化基础属性值
     */
    public static AftApplyOrderLine initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static AftApplyOrderLine newTemStyle() {
        return new AftApplyOrderLine();
    }

    public static AftApplyOrderLineCondition newAftApplyOrderLineCondition(MultiValueMap<String, String> params) {

        AftApplyOrderLineCondition condition = new AftApplyOrderLineCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, AftApplyOrderLineCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, AftApplyOrderLineCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, AftApplyOrderLineCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, AftApplyOrderLineCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }

    public static AftApplyOrderLine newAftApplyOrderLineByAfterSaleDetail(AfterSaleDetail afterSaleDetail){
        AftApplyOrderLine aftApplyOrderLine = new AftApplyOrderLine();
        aftApplyOrderLine.setUuid(UUID.fromString(afterSaleDetail.getUuid()));
        aftApplyOrderLine.setSerAfterSaleType(afterSaleDetail.getSerAfterSaleType());
        aftApplyOrderLine.setSerEditorContent(afterSaleDetail.getSerEditorContent());
        aftApplyOrderLine.setSerAfterSaleId(UUID.fromString(afterSaleDetail.getSerAfterSaleId()));
        aftApplyOrderLine.setAfterSaleNo(afterSaleDetail.getAfterSaleNo());
        aftApplyOrderLine.setSerAuditState(afterSaleDetail.getSerAuditState());
        aftApplyOrderLine.setSerUserReason(afterSaleDetail.getSerUserReason());


        if(VE.isNotEmpty(afterSaleDetail.getFkBrandUuid()))
            aftApplyOrderLine.setFkBrandUuid(UUID.fromString(afterSaleDetail.getFkBrandUuid()));
        aftApplyOrderLine.setSerGoodsBrandName(Optional.ofNullable(afterSaleDetail.getSerGoodsBrandName()).orElse(""));
        if(VE.isNotEmpty(afterSaleDetail.getSerItemId()))
            aftApplyOrderLine.setSerItemId(UUID.fromString(afterSaleDetail.getSerItemId()));
        aftApplyOrderLine.setSerItemName(afterSaleDetail.getSerItemName());
        if(VE.isNotEmpty(afterSaleDetail.getSerItemPrice()))
            aftApplyOrderLine.setSerItemPrice(Optional.ofNullable(Double.valueOf(afterSaleDetail.getSerItemPrice())).orElse(null));
        aftApplyOrderLine.setSerItemImg(afterSaleDetail.getSerItemImg());
        if(VE.isNotEmpty(afterSaleDetail.getSerCount()))
            aftApplyOrderLine.setSerCount(Optional.ofNullable(Integer.valueOf(afterSaleDetail.getSerCount())).orElse(null));
        aftApplyOrderLine.setSerLogisticsNum(Optional.ofNullable(afterSaleDetail.getSerLogisticsNum()).orElse(""));
        aftApplyOrderLine.setSerLogisticsCompany(Optional.ofNullable(afterSaleDetail.getSerLogisticsCompany()).orElse(""));
        if(VE.isNotEmpty(afterSaleDetail.getSerMoney()))
            aftApplyOrderLine.setSerMoney(Optional.ofNullable(Double.valueOf(afterSaleDetail.getSerMoney())).orElse(null));
        if (VE.isNotEmpty(afterSaleDetail.getSerOrderDetailUuid()))
            aftApplyOrderLine.setSerOrderDetailUuid(UUID.fromString(afterSaleDetail.getSerOrderDetailUuid()));
        return aftApplyOrderLine;
    }

}
